(function($){
	$.fn.productInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		
		var editDialog= $('#editDialog',$this);
		//查询的下拉框
		$('#item',$this).combobox({
			data:TEA.getProductItemList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//编辑的下拉框
		$('#productItemID',editDialog).combobox({
			data:TEA.getProductItemList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:250
		})
		//单位
		$('#unitID',editDialog).combobox({
			url:'dict!selectByType.action?dataDictionaryType=unit',
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:250
		})
		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var productItemID = $('#item',$this).combobox('getValue');
			var productCode = $('#code',queryContent).val();
			var productName = $('#name',queryContent).val();
			
			var content = {productCode:productCode,productName:productName,productItemID:productItemID};
			
			$('#productList',$this).datagrid({
				queryParams:content,
				pageNumber:1
			});
		})
		//添加
		var onAdd = function(){
			$(editDialog).dialog('open');
		} 
		//修改
		var onUpdate = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$(editDialog).dialog('open');
			$('#productForm',editDialog).form('load',selectRow);
		}
		//刷新列表
		var onReload = function(){
			$('#productList',$this).datagrid('reload');
		}
		//加载列表
		$('#productList',$this).datagrid({
			url:"product!select.action",
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onAdd()}},
						{id:'update',text:'修改',iconCls:'icon-edit',handler:function(){onUpdate()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'productID',hidden:true},
				{field:'productItemID',hidden:true},
				{field:'unitID',hidden:true},
				{field:'productItemName',title:'商品类别',width:100,align:"center"},
				{field:'productCode',title:'商品编号',width:100,align:"center"},
				{field:'productName',title:'商品名称',width:200,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'productPrice',title:'单价',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"},
				{field:'currentQuantity',title:'当前库存数',width:100,align:"center"},
				{field:'note',title:'备注',width:300,align:"center"}
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onUpdate();
			},
			onLoadSuccess:function(){
				selectRow = null;
		 		selectIndex = null;
			}
		});
		
		var onSave = function(){
			var productItemID = $('#productItemID',editDialog).combobox('getValue');
			if(productItemID==null){
				$.messager.alert('提示','种类不能为空','warning');
				return;
			}
			var productCode = $('#productCode',editDialog).val();
			if($.trim(productCode)==''){
				$.messager.alert('提示','编号不能为空','warning');
				return;
			}
			var productName = $('#productName',editDialog).val();
			if($.trim(productName)==''){
				$.messager.alert('提示','名称不能为空','warning');
				return;
			}
			var productPrice = $('#productPrice',editDialog).numberbox('getValue');
			if(productPrice==null||parseFloat(productPrice)<=0){
				$.messager.alert('提示','单价必须为正数','warning');
				return;
			}
			var url = null;
			if($('#productID',editDialog).val()!=''){
				url= 'product!update.action';
			}else{
				url= 'product!add.action';
			}
			$('#productForm',editDialog).ajaxSubmit({
				url:url,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						var fn = function(){
							onReload();
							onExit();
						}
						$.messager.alert('提示','保存成功','info',fn);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		
		var onExit = function(){
			$(editDialog).dialog('close');
			$('#productForm',editDialog).form('clear');
		}
		
		$(editDialog).dialog({  
		    title: '编辑商品',  
		    width:600,
		    height:500,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
	}
})(jQuery);